package com.ns.school.service.edu.impl;

import com.ns.school.common.utils.JsonUtil;
import com.ns.school.po.edu.BookDB;
import com.ns.school.po.edu.LayuiResult;
import com.ns.school.mapper.edu.BookMapper;
import com.ns.school.service.edu.BookService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Slf4j
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;

    @Override
    public Integer updBook(BookDB bookDB) {
        return bookMapper.updBook(bookDB);
    }

    @Override
    public List<BookDB> findAllBooks(BookDB bookDB) {
        return bookMapper.findAllBooks(bookDB);
    }

    @Override
    public Integer selectBook(BookDB bookDB) {
        return bookMapper.selectBook(bookDB);
    }

    @Override
    public Integer selectBookCode(BookDB bookDB) {
        return bookMapper.selectBookCode(bookDB);
    }

    @Override
    public BookDB editBook(Integer id2) {
        return bookMapper.editBook(id2);
    }




    @Override
    public Integer bookUpdate(Integer ids) {
        return bookMapper.bookUpdate(ids);
    }


    public BookDB selectByBookId(Integer id) {
        return bookMapper.selectById(id);
    }





    public void addBookMapper(LayuiResult result, BookDB bookDB) {


        Date date = new Date();
//        SimpleDateFormat dateFormate=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        bookDB.setCreateTime(date);
        bookDB.setDeleteFlag(0);
        Integer select = bookMapper.selectBook(bookDB);
        Integer select2 = bookMapper.selectBookCode(bookDB);
        if (select > 0) {
            result.setCode(500);
            result.setMsg("书本名称不可重复");
            //return result;
        }

        if (select2 > 0) {
            result.setCode(1062);
            result.setMsg("错误，图书编码已存在，无法添加");
            // return result;
        }
        if (StringUtils.isEmpty(bookDB.getBookName())) {
            result.setCode(500);
            result.setMsg("图书名称不能为空");
        } else {
            result.setCode(0);
            result.setMsg("success");
            Integer addBook = bookMapper.addBook(bookDB);
        }

    }

    @Transactional
    //删除书籍
    public void deleteBooks(LayuiResult result, BookDB bookDB) {
        int deletecount = bookMapper.deleteBook(bookDB.getId());
        if (deletecount == 1) {
            result.setCode(0);
            result.setMsg("删除成功！");
        } else {
            result.setCode(500);
            result.setMsg("删除失败");
        }
    }


    //上架
    @Transactional
    public void shelvesBooks(LayuiResult result, BookDB bookDB) {
        int shelvescount = bookMapper.shelvesBooks(bookDB.getId());
        if (shelvescount == 1) {
            result.setCode(0);
            result.setMsg("上架成功！");
        } else {
            result.setCode(500);
            result.setMsg("上架失败");
        }
    }


    //下架
    @Transactional
    public void frameBooks(LayuiResult result, BookDB bookDB) {
        int framecount =bookMapper.frameBooks(bookDB.getId());
        if (framecount == 1) {
            result.setCode(0);
            result.setMsg("下架成功！");
        } else {
            result.setCode(500);
            result.setMsg("下架失败");
        }
    }

    @Override
    public void findAll(BookDB bookDB) {
        List<BookDB> list = bookMapper.findAllBooks(bookDB);
        log.info(JsonUtil.toStr(list));
        Map<Integer,BookDB> map = new HashMap<>();
        for (BookDB book : list) {
            map.put(book.getBookId(),book);
        }

        for(Map.Entry<Integer,BookDB> entry:map.entrySet()){
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }

    }


}
